package com.huluobo.day11_im.login.view;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.biometrics.BiometricPrompt;
import android.os.Build;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.huluobo.day11_im.activity.MainActivity;
import com.huluobo.day11_im.R;
import com.huluobo.day11_im.base.BaseActivity;
import com.huluobo.day11_im.login.presenter.LoginContract;
import com.huluobo.day11_im.login.presenter.LoginPresenter;
import com.huluobo.day11_im.register.view.RegisterActivity;
import com.huluobo.day11_im.util.Md5Utils;
import com.huluobo.day11_im.util.SPUtils;

public class LoginActivity extends BaseActivity<LoginContract.View, LoginPresenter> implements LoginContract.View {
    private EditText username, password;
    private TextView textView;

    private ProgressDialog progressDialog;
    private BiometricPrompt biometricPrompt;
    private CancellationSignal cancellationSignal;


    @Override
    protected void initData() {
        presenter = new LoginPresenter();
    }

    @Override
    protected void initView() {
        username = findViewById(R.id.login_username);
        password = findViewById(R.id.login_password);
        if (SPUtils.getString(this, "username", "") != null) {
            username.setText(SPUtils.getString(this, "username", ""));
        }

        progressDialog = new ProgressDialog(this);
        textView = findViewById(R.id.toRegister);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            biometricPrompt = new BiometricPrompt.Builder(this)
                    .setTitle("指纹登录")
                    .setNegativeButton("取消", getMainExecutor(), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(LoginActivity.this, "取消", Toast.LENGTH_SHORT).show();
                        }
                    }).build();
        }

//每次调用authenticate时都必须传入一个新的CancellationSignal对象，否则会抛出异常
        cancellationSignal = new CancellationSignal();
        cancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
            @Override
            public void onCancel() {
                Toast.makeText(LoginActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void pointLogin(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            biometricPrompt.authenticate(cancellationSignal, getMainExecutor(),
                    new BiometricPrompt.AuthenticationCallback() {
                        @Override
                        public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
                            super.onAuthenticationSucceeded(result);
                            presenter.login("dd1","123");
                        }
                    });
        }
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_login;
    }

    public void login(View view) {
        String usernameString = username.getText().toString();
        String passwordString = password.getText().toString();
        presenter.login(usernameString, passwordString);
        SPUtils.putString(this, "username", usernameString);
        SPUtils.putString(this, "password", Md5Utils.encoderByMd5(passwordString));
    }

    @Override
    public void onUsernameError() {
        username.setError(getResources().getString(R.string.login_username_text));
    }

    @Override
    public void onPasswordError() {
        password.setError(getResources().getString(R.string.login_password_text));
    }

    @Override
    public void onStartLogin() {
        progressDialog.setMessage("开始登录");
        progressDialog.show();
    }

    @Override
    public void onLoginSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressDialog.dismiss();
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    public void onLoginFailed(String error) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressDialog.dismiss();
                Toast.makeText(LoginActivity.this, "登录失败:" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }
}